// Primitive全局管理器
class PrimitiveManager {
    private static instance: PrimitiveManager;
    private createdPrimitives: any[] = [];

    private constructor() {}

    public static getInstance(): PrimitiveManager {
        if (!PrimitiveManager.instance) {
            PrimitiveManager.instance = new PrimitiveManager();
        }
        return PrimitiveManager.instance;
    }

    // 添加Primitive
    public addPrimitive(primitive: any): void {
        this.createdPrimitives.push(primitive);
        console.log(`Primitive已添加到管理器，当前数量: ${this.createdPrimitives.length}`);
    }

    // 移除指定的Primitive
    public removePrimitive(primitive: any, viewer: any): void {
        const index = this.createdPrimitives.indexOf(primitive);
        if (index > -1) {
            viewer.scene.primitives.remove(primitive);
            this.createdPrimitives.splice(index, 1);
            console.log(`Primitive已从管理器移除，剩余数量: ${this.createdPrimitives.length}`);
        }
    }

    // 清除所有管理的Primitive
    public clearAllPrimitives(viewer: any): void {
        console.log(`开始清除 ${this.createdPrimitives.length} 个Primitive`);
        this.createdPrimitives.forEach(primitive => {
            viewer.scene.primitives.remove(primitive);
        });
        this.createdPrimitives = [];
        console.log('所有管理的Primitive已清除');
    }

    // 获取当前管理的Primitive数量
    public getCount(): number {
        return this.createdPrimitives.length;
    }

    // 获取所有管理的Primitive
    public getAllPrimitives(): any[] {
        return [...this.createdPrimitives];
    }
}

export default PrimitiveManager;
